/* 获取页面筛选条件（数据字典） */
import store from '../store'
import { Message } from 'element-ui'
import { pubRequire } from '@utils/public'
import { getActivitiesList } from '@/api/activeManagement/templatePage' // 接口
import { getCommons } from '@/api/commentManagement/commentManage' // 接口
let allDictionaries = ''

/* 字典管理-公共方法
 * */
export function sysNormalDisable(dictType = '', name = '') {
  return new Promise((resolve, reject) => {
    if (!allDictionaries) {
      //写在方法里，防止清理缓存后，页面报错
      // allDictionaries = JSON.parse(sessionStorage.getItem('dictionaries'))
      allDictionaries = JSON.parse(localStorage.getItem('dictionaries'))
    }
    let response = allDictionaries[dictType]
    resolve(response)
    // 存入vuex
    if (name && !store.state.valueTokey[name]) {
      let params = {
        obj: response,
        name: name
      }
      store.commit('setTypeNameCol', params)
    }
  })
}

/*获取关联活动
linkContentType//生活:LIFE;作品:WORKS
 * */
export function getActiveList(linkContentType) {
  return new Promise((resolve, reject) => {
    let obj = {
      type: 'lifes'
    }
    getActivitiesList(
      obj,
      (response) => {
        resolve(response)
      },
      (response) => {
        Message.warning(response.message)
        resolve([])
      }
    )
  })
}
/* 获取小手点赞数值 */
export function getPraiseBtn() {
  return new Promise((resolve, reject) => {
    let url = '/system/parameters/options'
    let obj = {
      configKey: 'praiseNumber'
    }
    pubRequire(
      'get',
      url,
      obj,
      (response) => {
        resolve(response)
      },
      (response) => {
        Message.warning(response.message)
        resolve('')
      }
    )
  })
}
/*获取审批状态码*/
export function getStatusCode(vid, obj) {
  return new Promise((resolve, reject) => {
    let url = `/platforms/polyv/${vid}/audit-status`
    pubRequire(
      'get',
      url,
      obj,
      (response) => {
        resolve(response)
      },
      (response) => {
        Message.warning(response.message)
        resolve('')
      }
    )
  })
}
/*获取保利威删除理由状态码*/
export async function getDelCode(vid) {
  let ary = await sysNormalDisable('poly_audit_reason')
  return new Promise((resolve, reject) => {
    let url = `/platforms/polyv/audit-reason`
    let obj = {
      vids: vid
    }
    pubRequire(
      'get',
      url,
      obj,
      (response) => {
        let cur = ary.find((item) => item.dictValue === response)
        if (cur) {
          resolve(cur.dictLabel)
        } else {
          resolve('')
        }
      },
      (response) => {
        Message.warning(response.message)
        resolve('')
      }
    )
  })
}
/* 获取随机评论内容 */
export function getRandomCon(obj) {
  return new Promise((resolve, reject) => {
    getCommons(
      obj,
      (response) => {
        resolve(response)
      },
      (response) => {
        Message.warning(response.message)
        resolve('')
      }
    )
  })
}

/* 获取马甲 */
export function getVestList(userId) {
  return new Promise((resolve, reject) => {
    let url = '/robots/options'
    pubRequire(
      'get',
      url,
      { userId },
      (response) => {
        resolve(response.rows)
      },
      (response) => {
        Message.warning(response.message)
        resolve('')
      }
    )
  })
}
/*运营人员下的随机马甲*/
export function getRandomRobotId(userId) {
  return new Promise((resolve, reject) => {
    let url = `/robots/random?operatorId=${userId}`
    pubRequire(
      'get',
      url,
      '',
      (response) => {
        resolve(response.robotId)
      },
      (response) => {
        Message.warning(response.message)
        resolve('')
      }
    )
  })
}
/*运营人员下的随机马甲接口*/
export function getRandomRobot(userId, callBack, errBack) {
  return pubRequire(
    'get',
    `/robots/random?operatorId=${userId}`,
    '',
    callBack,
    errBack
  )
}
/* 获取帖子活动列表 */
export function getActivities() {
  return new Promise((resolve, reject) => {
    let obj = {
      type: 'posts'
    }
    getActivitiesList(
      obj,
      (response) => {
        resolve(response)
      },
      (response) => {
        Message.warning(response.message)
        resolve([])
      }
    )
  })
}
/* 作品、生活，列表筛选条件：活动列表
 * linkContentType-生活：LIFE;作品：WORKS
 * searchContent-活动名称（非必传）
 * */
export function getWorkListActivities(linkContentType, obj = '') {
  return new Promise((resolve, reject) => {
    let url = `/activities/options?type=${linkContentType}`
    pubRequire(
      'get',
      url,
      obj,
      (response) => {
        resolve(response)
      },
      (response) => {
        Message.warning(response.message)
        resolve([])
      }
    )
  })
}
